import 'package:boss/main.dart';
import 'package:boss/models/offers.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class OffersDetailProvider extends ChangeNotifier {
  Offers data;
  int type;

  OffersDetailProvider(
    this.data,
    this.type,
  );

  updateStatus() async {
    try {
      showLoadingToast();
      var res = await api.updateStoreDiscount({
        'status': type == OffersType.over ? 2 : 0,
        'discountId': data.id,
      });
      if (!res.successful()) {
        toast(res.msg);
        return;
      }
      var offersProvider = Provider.of<OffersProvider>(
        GlobalPageContext.instance.context,
        listen: false,
      );
      offersProvider.backstageRefresh();
      toast('修改状态成功');

      if (type == OffersType.over) {
        Navigator.pop(GlobalPageContext.instance.context);
      } else if (type == OffersType.processing) {
        type = OffersType.over;
        notifyListeners();
      }
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }
}
